package org.seven.jrdp.web.mgt.sys;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.seven.jrdp.biz.sys.model.SysUser;
import org.seven.jrdp.biz.sys.service.SysOrganService;
import org.seven.jrdp.biz.sys.service.SysUserService;
import org.seven.jrdp.commons.exception.WebException;
import org.seven.jrdp.commons.model.Option;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.ServletUtils;
import org.seven.jrdp.commons.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.google.zxing.EncodeHintType;

import net.glxn.qrgen.javase.QRCode;

@RestController
@RequestMapping("/mgt/sys/user")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysOrganService sysOrganService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/sys/user/user_" + view);
	}

	@PostMapping("/find")
	public PageInfo<SysUser> find(PageRequest pageRequest, HttpServletRequest request) {
		Map<String, Object> map = ServletUtils.getSearchParams(request);
		sysOrganService.addOrganCode(map, ShiroUtils.getUser().getOrgan().getCode());
		PageInfo<SysUser> page = sysUserService.find(map, pageRequest);
		return page;
	}

	@RequestMapping("/get")
	public SysUser get(String id) {
		SysUser sysUser = sysUserService.get(id);
		return sysUser;
	}

	@PostMapping("/insert")
	public Result insert(SysUser sysUser) {
		sysUserService.insert(sysUser);
		return Result.SUCCESS;
	}

	@PostMapping("/update")
	public Result update(SysUser sysUser) {
		sysUserService.update(sysUser);
		return Result.SUCCESS;
	}

	@PostMapping("/delete")
	public Result delete(String id) {
		sysUserService.delete(id);
		return Result.SUCCESS;
	}

	@RequestMapping("/password")
	public Result password(String id) {
		sysUserService.resetPassword(id);
		return Result.SUCCESS;
	}

	@RequestMapping("/get/{account}")
	public SysUser getByAccount(@PathVariable String account) {
		SysUser sysUser = sysUserService.getByAccount(account);
		return sysUser;
	}

	@RequestMapping("/option")
	public List<Option> option(String organCode, String roleCode) {
		List<Option> list = sysUserService.findOption(organCode, roleCode);
		return list;
	}

	@RequestMapping("/all")
	public List<Option> all() {
		List<Option> list = sysUserService.findAll();
		return list;
	}

	@RequestMapping("/map")
	public List<Map<String, Object>> map() {
		List<Map<String, Object>> list = sysUserService.findMap();
		return list;
	}

	@RequestMapping("/qrcode/{id}/{size}")
	public void qrcode(@PathVariable String id, @PathVariable String size, HttpServletRequest request, HttpServletResponse response) {
		SysUser sysUser = sysUserService.get(id);
		int width = 0;
		int height = 0;
		try {
			width = Integer.parseInt(size.split("\\*")[0]);
			height = Integer.parseInt(size.split("\\*")[1]);
		} catch (Exception e) {
			throw new WebException("二维码尺寸格式错误");
		}
		File file = QRCode.from(id).withSize(width, height).withHint(EncodeHintType.MARGIN, 0).file();
		ServletUtils.download(file, sysUser.getAccount() + ".png", request, response);
		file.delete();
	}
}